<template>
  <!-- 对话框组件：定义标题动态绑定；控制对话框显隐，双向绑定；对话框宽度；关闭时销毁对话框实例；点击模态框不关闭对话框；关闭前的回调函数绑定到了cancel -->
  <el-dialog
    :title="title"
    :visible.sync="openSheet"
    width="50%"
    :destroy-on-close="true"
    :close-on-click-modal="false"
    :before-close="cancel"
  >
    <el-form ref="form" :model="form" :rules="rules" :label-width="labelWidth">
      <el-row>
        <el-col :span="12">
          <!-- 表单组件：prop表单域的model字段传入form组件model中的字段；error表单与验证错误信息 -->
          <el-form-item :label="$t('standardCurrency')" prop="standard_curr" :error="error.standard_curr">
            <el-select v-model="form.standard_curr" clearable style="width: 240px;" allow-create filterable :placeholder="$t('inputstandardCurrency')">
              <el-option v-for="status in currencyOptions" :key="status.currency" :label="status.currency" :value="status.currency">
                <span style="float: left">{{ status.currency }}</span>
                <span style="float: right; color: #8492a6; font-size: 13px">{{ status.currency_name }}</span>
              </el-option>
            </el-select>
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item :label="$t('quoteCurrency')" prop="quote_curr" :error="error.quote_curr">
            <el-select v-model="form.quote_curr" clearable allow-create style="width: 240px;" filterable :placeholder="$t('inputquoteCurrency')">
              <el-option v-for="status in currencyOptions" :key="status.currency" :label="status.currency" :value="status.currency">
                <span style="float: left">{{ status.currency }}</span>
                <span style="float: right; color: #8492a6; font-size: 13px">{{ status.currency_name }}</span>
              </el-option>
            </el-select>
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item :label="$t('rateDate')" prop="rate_date" :error="error.rate_date">
            <el-date-picker v-model="form.rate_date" type="date" style="width: 240px;" value-format="yyyy-MM-dd" :placeholder="$t('rateDate')" />
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item :label="$t('rate')" prop="exchange_rate" :error="error.exchange_rate">
            <el-input v-model="form.exchange_rate" style="width: 240px;" :placeholder="$t('inputrate')" />
          </el-form-item>
        </el-col>
      </el-row>
    </el-form>
    <!-- 底部容器：slot命名插槽用于将内容分发到子组件中指定名称的区域，这里专门用于定义对话框的 底部区域（ -->
    <div slot="footer" class="dialog-footer">
      <el-button v-if="createFlag" type="primary" :disabled="!isModified" @click="handleCreate">{{
        $t('create')
      }}</el-button>
      <el-button v-else type="primary" :disabled="!isModified" @click="handleUpdate">{{
        $t('update')
      }}</el-button>
      <el-button @click="cancel">{{ $t('cancel') }}</el-button>
    </div>
  </el-dialog>
</template>

<script>
import sheetMixin from '@/mixins/SheetMixin'
import { createCurrencyRateApi, updateCurrencyRateApi } from '../api/currencyrate.api';
import { getDropDownCodeTableListApi } from '@/api/common/common'
import { getCurrencyListApi } from '@/views/master/currency/api/currency.api';

export default {
  mixins: [sheetMixin],
  data() {
    return {
      formRef: 'form', // 为 el-form 组件定义一个引用名称，通过这个名称可以在组件的逻辑里获取表单实例，进而调用表单的方法，像表单验证、重置表单等操作。
      createTitle: this.$t('currencyRateCreateTitle'),
      updateTitle: this.$t('currencyRateUpdateTitle'),
      currencyOptions: [],
      actionApi: {
        'create': createCurrencyRateApi,
        'update': updateCurrencyRateApi
      },
      // 在 data 中定义 form 和 error 对象及其相应的字段。为了确保表单数据的正确绑定、显示和提交，以及错误信息的正常展示
      form: {
        standard_curr: undefined,
        quote_curr: undefined,
        rate_date: undefined,
        exchange_rate: undefined
      },
      error: {
        standard_curr: undefined,
        quote_curr: undefined,
        rate_date: undefined,
        exchange_rate: undefined
      },
      rules: {
        standard_curr: [{ required: true, message: this.$t('standardCurrencyRequired'), trigger: 'blur' }],
        quote_curr: [{ required: true, message: this.$t('quoteCurrencyRequired'), trigger: 'blur' }],
        rate_date: [{ required: true, message: this.$t('rateDateDateRequired'), trigger: 'blur' }],
        exchange_rate: [{ required: true, message: this.$t('exchangeRateRequired'), trigger: 'blur' }]
      }
    };
  },
  computed: {
    labelWidth() {
      return this.$i18n.locale === 'en' ? '150px' : '80px';
    }
  },
  methods: {
    initialize() {
      // this.handleCurrencyOptions()
      this.getCurrncyList()
    },
    // 获取币种下拉框数据（目前已经不再使用，改用下面的从主表中获取的方法）
    // 异步函数始终返回一个 Promise 对象，
    async handleCurrencyOptions() {
      var params = { 'model': 'CurrencyRateModel', 'field': 'currency_option' }
      // 使用 await 关键字来暂停函数的执行，等待一个 Promise 被解决（resolved）或被拒绝（rejected）。在等待期间，函数不会阻塞主线程，其他代码可以继续执行
      var resp = await getDropDownCodeTableListApi(params)
      if (resp.success) {
        this.currencyOptions = resp.data;
      } else {
        this.targaMessageBox(this, resp.msg);
      }
    },
    // 在币种主表中获取币种信息
    async getCurrncyList() {
      var resp = await getCurrencyListApi()
      if (resp.success) {
        this.currencyOptions = resp.data.results;
      } else {
        this.targaMessageBox(this, resp.msg);
      }
    },
    // 普通函数的返回值类型取决于函数内部的 return 语句
    handleCreate() {
      this.submitForm('create', this.$t('createSuccess', { title: this.$t('standardCurrency'), value: this.form.standard_curr }))
    },
    handleUpdate() {
      this.submitForm('update', this.$t('updateSuccess', { title: this.$t('standardCurrency'), value: this.form.standard_curr }))
    }
  }
};
</script>

<style scoped>
</style>
